## Figures for Null Results Report: Nudges for Citizenship Fee Waivers
## This file should be run after the STATA replication file. It uses the text file nudgeeffects.txt, which is created by the STATA replication file. 

rm(list=ls())
library(foreign)
library(ggplot2)
library(ggthemes)
library(RColorBrewer)
library(gridExtra)

###



file_location_name <- "tables/nudgeeffects.txt"

d <- read.table(file_location_name, sep="\t", header=TRUE, row.names = 1)
figname <- c("figNudge.pdf")
d$X.1 <- NULL

d$name <- rownames(d)
d$lb <- d$pe-1.96*d$se
d$ub <- d$pe+1.96*d$se
d

d1 <- d[1:3,]
d1$name <- factor(d1$name,levels=unique(d1$name)[3:1],
                    labels=c("Control\n(N=106)",
                             "Enhanced\n(N=218)",
                             "Invitation\n(N=237)")[3:1])
  
  d1
  
  p <- ggplot(data = d1, aes(x = name, y = pe, fill = name,ymax=lb,ymin=ub)) + 
    ggtitle("Application Rates in Treatment and Control Groups") 
  p <- p + geom_bar(stat = "identity",position = "dodge",width=0.35) + geom_errorbar(position = "dodge",width=0.15) +  coord_flip()
  p <- p + scale_fill_manual(values=c(rep("indianred1",2),"lightseagreen"),guide = FALSE)  
  p <- p + scale_y_continuous(name="Applied for Naturalization (%)",breaks=seq(0,60,10), 
                              labels=seq(0,60,10)        , limits = c(0,60))
  p <- p + theme(axis.title.y = element_blank(),
                 axis.title=element_text(size=10,face="bold"),
                 axis.text=element_text(size=10,face="bold"),
                 title=element_text(size=12,face="bold"))
  p1 <- p
  p1
  d1
  

d  
  d1 <- d[4:nrow(d),]
  d1
  d1$name <- factor(d1$name,levels=unique(d1$name)[2:1],labels=c("Enhanced", "Invitation")[2:1])
  d1
  
  p <- ggplot(data = d1, aes(x = name, y = pe, fill = name,ymax=lb,ymin=ub)) + ggtitle("Intention-to-treat Effect of Nudges")
  p <- p + geom_point(pch=21,size=4,col="cornflowerblue") + geom_errorbar(position = "dodge",width=0,size=1.5,col="cornflowerblue") + coord_flip()
  p <- p + scale_fill_manual(values=rep("cornflowerblue",5),guide = FALSE)  + ylim(-40,40) 
  p <- p + theme(axis.title.y = element_blank(),
                   axis.title=element_text(size=10,face="bold"),
                   axis.text=element_text(size=10,face="bold"),
                   title=element_text(size=12,face="bold"),
                   aspect.ratio = .3)
  p <- p + ylab("Change in Application Rate (%)") + geom_hline(yintercept=0,col="slategrey",lty=2)
  p2 <- p
  p2
  
  file_output_location_name <- "figures/figureB1.pdf"
  
  p3 <- arrangeGrob(p2, p1)
  ggsave(file = file_output_location_name, p3, device = "pdf")


